
/*
 *  Openmysee
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
//

#include "stdafx.h"
#include "MMSServer.h"
#include "AddMMSDlg.h"
#include "ConfigFile.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAddMMSDlg dialog


CAddMMSDlg::CAddMMSDlg(CMMSPool *pMMSPool, CWnd* pParent /*=NULL*/)
	: CDialog(CAddMMSDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAddMMSDlg)
	m_strAddr = _T("");
	m_strChannel = _T("");
	m_bAddCurItemOnClose=TRUE;
	m_BDeleteRegEntriesOnClear=TRUE;
	//}}AFX_DATA_INIT

	m_pMMSPool = pMMSPool;
}


void CAddMMSDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAddMMSDlg)
	DDX_Text(pDX, IDC_COMBO_MMSADDR, m_strAddr);
	DDX_Control(pDX,IDC_COMBO_MMSADDR,m_mcomboItems);
	DDX_Text(pDX,IDC_EDIT_CHNLNAME,m_strChannel);
	DDX_Check(pDX,IDC_BTN_CLEAR_HISTORY2,m_BDeleteRegEntriesOnClear);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CAddMMSDlg, CDialog)
	//{{AFX_MSG_MAP(CAddMMSDlg)
	ON_BN_CLICKED(IDC_BTN_CLEAR_HISTORY2, OnBtnClearHistory2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAddMMSDlg message handlers

void CAddMMSDlg::OnOK() 
{  	
	
	/////////////Get the channel name  and add it 
	string strChannel;
	CString str;
	GetDlgItemText(IDC_EDIT_CHNLNAME, str);
	if(str.IsEmpty())
	{
		AfxMessageBox("you must enter the channel name!");
		return;
	}

	strChannel.append(str);
  
	//////////////Get The MMS address  Add  Add it 
	string strAddr;
	GetDlgItemText(IDC_COMBO_MMSADDR, str);
	if(str.IsEmpty())
	{
		AfxMessageBox("you must enter the MMS address!");
		return;
	}

	strAddr.append(str);
   	
    /////////////////Get The ModulePath  And  Write the File
	CString str_path;
	GetModuleFileName(NULL,str_path.GetBuffer(MAX_PATH),MAX_PATH);
	str_path.ReleaseBuffer();
	int index = str_path.ReverseFind('\\');
    str_path = 	str_path.Left(index+1);

  ////////////////Save the MMSAddress 
   CString Items;
   m_mcomboItems.GetWindowText(Items);
   m_mcomboItems.AddString(Items);
   m_mcomboItems.SaveHistory(m_bAddCurItemOnClose);
 
  
	////////////// From Dialog Get Data And Write The file
	CString Name;
	CString MMSAddress;
	CString spIP;
	GetDlgItem(IDC_EDIT_CHNLNAME)->GetWindowText(Name);
	GetDlgItem(IDC_COMBO_MMSADDR)->GetWindowText(MMSAddress);
	GetDlgItem(IDC_EDIT_SPADDR)->GetWindowText(spIP);

	//write to CaptureServer.cfg
	FILE *pfile = fopen(str_path+"CaptureServer.cfg","w");
	fprintf(pfile,"%s\n","[CaptureServer]");
	fprintf(pfile,"%s%s\n","channelName=",Name);
	fprintf(pfile,"%s\n","userID=10040");
	fprintf(pfile,"%s\n","password=ccc602a8ef81836cdd332ce0d31ef268");
	fprintf(pfile,"%s%s\n","spAddress=",spIP);
	fprintf(pfile,"%s\n","ReconnectTime=120");
	fprintf(pfile,"%s%s\n","MMSAddress=",MMSAddress);
	fclose(pfile);
		
	UpdateData(TRUE);
 
	CDialog::OnOK();
}


BOOL CAddMMSDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	
    m_mcomboItems.LoadHistory(_T("HistoryCombos"), _T("HistoryItem"));
    
   ////// Get The ModulePath And Write The File 
    CString str_path;
	GetModuleFileName(NULL,str_path.GetBuffer(MAX_PATH),MAX_PATH);
	str_path.ReleaseBuffer();
	int index = str_path.ReverseFind('\\');
    str_path= 	str_path.Left(index+1);
   
	ConfigFile myfile((str_path+"CaptureServer.cfg").GetBuffer(MAX_PATH));
	string spIP=myfile.Value("CaptureServer","spAddress");
	string Name =myfile.Value("CaptureServer","channelName");
	string MMSAdress=myfile.Value("CaptureServer","MMSAddress");

	GetDlgItem(IDC_EDIT_SPADDR)->SetWindowText(spIP.data());
	GetDlgItem(IDC_EDIT_CHNLNAME)->SetWindowText(Name.data());
	GetDlgItem(IDC_COMBO_MMSADDR)->SetWindowText(MMSAdress.data());

	return TRUE; 
	// return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}



void CAddMMSDlg::OnBtnClearHistory2() 
{
	// TODO: Add your control notification handler code here
 	UpdateData(TRUE);
 	m_mcomboItems.ClearHistory(m_BDeleteRegEntriesOnClear);
}
